// Sample Java method to query a VHOME HTTP-GET API
// example invocation : queryGetApi("ListAllStreamTypes", "http://blizzard.cs.uwaterloo.ca:8080/vhome")

 String queryGetAPI(String api, String vhome)
{

	String urlStr = vhome +"/"+ api; String result = null;
	try
	{
	URL url = new URL(urlStr);
	URLConnection conn = url.openConnection ();

	// Get the response
	BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	StringBuffer sb = new StringBuffer();
	String line;

		while ((line = rd.readLine()) != null	)
		{
		sb.append(line);
		}
	rd.close();
	result = sb.toString();
	return result;

	}
	catch (Exception e)
	{
	e.printStackTrace();
	return "exception";
	}

}


//  Sample Java method to query a VHOME HTTP-POST API
// example invocation
//HashMap<String,String> m = new HashMap<String,String>();
//       m.put("dsname","Datastream1" );
//       m.put("descriptor","description of the data stream" );
//       m.put("stid", "1" );
//       m.put("granularity", "3600000");
//       m.put("symbol", "kWh");
// queryPostAPI("AddDataStream", "http://blizzard.cs.uwaterloo.ca/vhome:8080", m );

private String queryPostAPI(String api, String vhome, HashMap<String,String> m)

{

try {
String data="";
Iterator<Entry<String, String>> it = m.entrySet().iterator();

    while (it.hasNext()) {

        Map.Entry pairs = (Map.Entry)it.next();
        data+=URLEncoder.encode(pairs.getKey().toString(), "UTF-8") + "=" + URLEncoder.encode(pairs.getValue().toString(), "UTF-8") + "&";
        it.remove(); // avoids a ConcurrentModificationException

    }

    // Send data
    URL url = new URL(vhome+"/"+api);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line="", resp ="";
    while ((line = rd.readLine()) != null) {
        // Process line...
    resp+=line;
    }

    wr.close();
    rd.close();
    return line;

} catch (Exception e) {

return "exception";

}

}


// Sample Java method to query VHOME HTTP-POST API accepting JSON array input, e.g. AddTimSeries
// Example invocation : queryJSONPostAPI("AddTimeSeries", "http://blizzard.cs.uwaterloo.ca:8080/vhome", "[{\"timestamp\":12982121000,\"value\":19.21}]")

private String queryJSONPostAPI(String api, String vhome, String data) {
		try {
			URL url = new URL(vhome+"/"+api);
			System.out.println(url.toString());
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);

			conn.setRequestProperty("Content-Type", "application/json");

			OutputStream os = conn.getOutputStream();
			os.write(data.getBytes());
			os.flush();

			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				throw new RuntimeException("Failed : HTTP error code : "
					+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String ret = "", output="";
			//System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				ret += output;
			}

			conn.disconnect();
			return ret;

		}
		catch(Exception e)
		{
			e.printStackTrace();
			return "json_exception";
		}
	}



